const http = require('http');
const fs = require('fs');
const url = require('url');

const io = require('socket.io');
const sql = require('mysql');

// 创建数据池 连接数据库
let db = sql.createPool({host: 'localhost',user: 'root',password: '',database: 'webtest'});

// 创建服务 
let server = http.createServer((req,res)=>{
    fs.readFile(`www${req.url}`,(err,data)=>{
        if (err) {
            res.writeHeader(404);
            res.write('Not Found');
        } else {
            res.write(data);
        }

        res.end();
    });
    

});

server.listen(2222,()=>{
    console.log('success on 2222');
});


// 创建 socket 服务
let wsServer = io.listen(server);


/***
 * websocket 接口 
 * 'reg' user pass -> 'reg_ret' code msg 
 * 'login' user pass -> 'login_ret' code msg
 */


wsServer.on('connection',sock=>{
    sock.on('reg',(user,pass)=>{

    });

    sock.on('login',(user,pass)=>{

    });
})




